import http from '../../util/http';
import * as api from '../../util/api';

const state ={
    GoodsCarList:[]
};
const actions={
    queryShopCart({commit}){  //查询购物车列表
        let clientId=sessionStorage.getItem('clientId')
        return http({
            url:api.queryShopCart,          //4.9查询购物车列表
            method:'post',
            data:{
               clientId:"7bff3ee541e746f786623dfc7bb39cfb"
            }
        }).then(res=>{
            commit('getGoodsCar',res.data.data)
            console.log(res.data.data)
            return res.data.data //将数据返回给页面，处理异步
        }).catch(err=>{
            console.log(err)
        })
    },
    queryShopCartByKeyword({commit},command){          //4.10购物车排序
        if(command===undefined){
            command=0;
        }
        let clientId=sessionStorage.getItem('clientId')
        return http({
            url:api.queryShopCartByKeyword,
            method:'post',
            data:{
                clientId:"7bff3ee541e746f786623dfc7bb39cfb",
                condition:{
                    selectValue:command,
                }
            }
        }).then(res=>{
            commit('queryShopCart',res.data.data)
            return res.data.data //将数据返回给页面，处理异步
        })
    },
    updateShopCart({commit},a){          //4.11修改购物车商品数量
        let clientId=sessionStorage.getItem('clientId')
        // console.log(a)
        return http({
            url:api.updateShopCart,
            method:'post',
            data:{
                clientId:"7bff3ee541e746f786623dfc7bb39cfb",
                goodsId:a.id,
                goodsNumber:a.number,
            }
        }).then(res=>{
            commit('queryShopCart',res.data.code)
            return res.data.code
        })
    },
    deleteShopCart({commit},id){          //4.12删除购物车商品
        let clientId=sessionStorage.getItem('clientId')
        return http({
            url:api.deleteShopCart,
            method:'post',
            data:{
                clientId:"7bff3ee541e746f786623dfc7bb39cfb",
                goodsId:id,
            }
        }).then(res=>{
            commit('delShopCart',id)
            return res.data.code
        })
    },
    emptyShopCart({commit}){          //4.13清空购物车商品
        let clientId=sessionStorage.getItem('clientId')
        return http({
            url:api.emptyShopCart,
            method:'post',
            data:{
                clientId:"7bff3ee541e746f786623dfc7bb39cfb",
            }
        }).then(res=>{
            commit('emptyShopCart',res.data.code)
            return res.data.code
        })
    },
};

const mutations ={
    getGoodsCar(state,obj){    //加入购物车
        state.GoodsCarList = obj
    },
    queryShopCart(state,obj){       //排序
        state.GoodsCarList = obj
    },
    delShopCart(state,id){       //排序
        console.log(id)
        let num
        state.GoodsCarList.filter((item,i)=>{
            if(item.goodsId==id){
                num=i
            }
        })
        console.log(num)
        state.GoodsCarList.splice(num,1)
        console.log(state.GoodsCarList)
    },
    emptyShopCart(state,obj){       //清空
        state.GoodsCarList = []
    },

};
export default{
    state,
    actions,
    mutations,
}